package uni.UNIBEE7B8D.Presenter;

import android.util.Log;

import com.google.gson.Gson;

import org.json.JSONException;

import java.text.ParseException;

import javax.xml.transform.OutputKeys;

import uni.UNIBEE7B8D.CommonTool.HttpUrl;
import uni.UNIBEE7B8D.CommonTool.OkHttpUtil;
import uni.UNIBEE7B8D.Enty.HomeClass;
import uni.UNIBEE7B8D.Enty.HomeFriend;
import uni.UNIBEE7B8D.Enty.HomeKecheng;
import uni.UNIBEE7B8D.Enty.HomeMenu;
import uni.UNIBEE7B8D.Enty.HomePage;
import uni.UNIBEE7B8D.Enty.HomeZhibo;
import uni.UNIBEE7B8D.Enty.Result;
import uni.UNIBEE7B8D.view.Study.StudyContract;

public class StudyPersenter implements StudyContract.StudyPersenter {

    StudyContract.StudyView studyView;

    public StudyPersenter(StudyContract.StudyView studyView) {
        this.studyView = studyView;
    }

    @Override
    public void GetData() {
        new OkHttpUtil().getJson(HttpUrl.homePage, new OkHttpUtil.HttpCallBack() {
            @Override
            public void onSusscess(String data) throws JSONException, ParseException {
                HomePage result = new Gson().fromJson(data,HomePage.class);
                if(result.isSuccess()){
                    studyView.GetData(result);
                }else{
                    studyView.Toast(result.getMessage());
                }
            }
        });

    }

    @Override
    public void GetZaikan(String user_id) {
        Log.d("==",HttpUrl.homeZaikan+"?user_id="+user_id);
        new OkHttpUtil().getJson(HttpUrl.homeZaikan+"?user_id="+user_id, new OkHttpUtil.HttpCallBack() {
            @Override
            public void onSusscess(String data) throws JSONException, ParseException {
                Log.d("==",data);
                HomeFriend homeFriend  = new Gson().fromJson(data,HomeFriend.class);
                if(homeFriend.isSuccess()){
                    studyView.GetZaikan(homeFriend);
                }else{
                    studyView.Toast(homeFriend.getMessage());
                }

            }

            @Override
            public void onError(String meg) {
                super.onError(meg);
                studyView.Toast(meg);
            }
        });
    }

    @Override
    public void GetZhibo() {
        new OkHttpUtil().getJson(HttpUrl.homeZhibo, new OkHttpUtil.HttpCallBack() {
            @Override
            public void onSusscess(String data) throws JSONException, ParseException {
                HomeZhibo homeZhibo = new Gson().fromJson(data,HomeZhibo.class);
                if(homeZhibo.isSuccess()){
                    studyView.GetZhibo(homeZhibo);
                }else{
                    studyView.Toast(homeZhibo.getMessage());
                }
            }
        });
    }

    @Override
    public void GetKecheng(int pageNo, int pageSize) {
        new OkHttpUtil().getJson(HttpUrl.homeKecheng + "?pageNo=" + pageNo + "&pageSize=" + pageSize, new OkHttpUtil.HttpCallBack() {
            @Override
            public void onSusscess(String data) throws JSONException, ParseException {
                HomeKecheng homeKecheng = new Gson().fromJson(data,HomeKecheng.class);
                if(homeKecheng.isSuccess()){
                    studyView.GetKecheng(homeKecheng);
                }else{
                    studyView.Toast(homeKecheng.getMessage());
                }
            }
        });
    }

    @Override
    public void GetClass(int pageNo, int pageSize) {
        new OkHttpUtil().getJson(HttpUrl.homeClass + "?pageNo=" + pageNo + "&pageSize=" + pageSize, new OkHttpUtil.HttpCallBack() {
            @Override
            public void onSusscess(String data) throws JSONException, ParseException {
                HomeClass homeClass = new Gson().fromJson(data,HomeClass.class);
                if(homeClass.isSuccess()){
                    studyView.GetClass(homeClass);
                }else{
                    studyView.Toast(homeClass.getMessage());
                }
            }
        });
    }

    @Override
    public void GetMenu(int aspect, int pageNo, int pageSize, int type) {
        new OkHttpUtil().getJson(HttpUrl.daohang +"?column=rank&order=asc", new OkHttpUtil.HttpCallBack() {
            @Override
            public void onSusscess(String data) throws JSONException, ParseException {
                HomeMenu homeMenu = new Gson().fromJson(data,HomeMenu.class);
                if(homeMenu.isSuccess()){
                    studyView.GetMeny(homeMenu);
                }else {
                    studyView.Toast(homeMenu.getMessage());
                }
            }
        });
    }


}
